using System.Collections.Generic;
using ProtoModels;
using UnityEngine;

public class Persona
{
	public class LookSelection
	{
		public CustomisationConfig.LookSetType type;

		public CustomisationConfig.Look selectedLook;

		protected LookSelection()
		{
		}

		public LookSelection(CustomisationConfig.LookSet set, int activeLookIndex)
		{
			type = set.type;
			selectedLook = ((!IsSetIndexValid(set, activeLookIndex)) ? null : set.looks[activeLookIndex]);
		}

		public LookSelection(CustomisationConfig.LookSet set, CustomisationConfig.Look selectedLook)
		{
			type = set.type;
			this.selectedLook = selectedLook;
		}

		public static LookSelection GetLookSelectionForRemote(CustomisationConfig.LookSet set, int activeLookIndex)
		{
			LookSelection lookSelection = new LookSelection();
			lookSelection.type = set.type;
			lookSelection.selectedLook = ((activeLookIndex < 0 || set.looks.Count <= activeLookIndex) ? null : set.looks[activeLookIndex]);
			return lookSelection;
		}

		private bool IsSetIndexValid(CustomisationConfig.LookSet set, int activeLookIndex)
		{
			return activeLookIndex >= 0 && set.looks.Count > activeLookIndex && (PlayerInventory.instance.isOwned(set.looks[activeLookIndex].lookName) || set.looks[activeLookIndex].price <= 0);
		}

		public int GetActiveLookIndex()
		{
			return CustomisationConfig.instance.GetLookIndex(type, selectedLook);
		}

		public LookSelection Clone()
		{
			LookSelection lookSelection = new LookSelection();
			lookSelection.type = type;
			lookSelection.selectedLook = selectedLook;
			return lookSelection;
		}
	}

	public enum Gender
	{
		Male = 0,
		Female = 1
	}

	private List<LookSelection> config = new List<LookSelection>();

	public Gender gender { get; protected set; }

	public List<LookSelection> myConfig
	{
		get
		{
			return config;
		}
	}

	public Persona(GGPlayerSettings ps)
	{
		config.Clear();
		gender = (Gender)ps.Model.genderSelect;
		foreach (CustomisationConfig.LookSet lookSet in CustomisationConfig.instance.lookSets)
		{
			if (lookSet.looks.Count > 0)
			{
				LookSelection lookSelectionForPlayerSettingsAndSet = GetLookSelectionForPlayerSettingsAndSet(lookSet, ps);
				if (lookSelectionForPlayerSettingsAndSet.selectedLook == null || !lookSelectionForPlayerSettingsAndSet.selectedLook.IsAvailable(this))
				{
					lookSelectionForPlayerSettingsAndSet.selectedLook = lookSet.GetDefaultLookForPersona(this);
				}
				config.Add(lookSelectionForPlayerSettingsAndSet);
			}
		}
	}

	public Persona(MatchParameters.MatchParams.MultiplayerParams mp)
	{
		config.Clear();
		gender = (Gender)mp.remoteLookGender;
		foreach (CustomisationConfig.LookSet lookSet in CustomisationConfig.instance.lookSets)
		{
			if (lookSet.looks.Count > 0)
			{
				LookSelection lookSelectionForMultiplayerParamsAndSet = GetLookSelectionForMultiplayerParamsAndSet(lookSet, mp);
				if (lookSelectionForMultiplayerParamsAndSet.selectedLook == null || !lookSelectionForMultiplayerParamsAndSet.selectedLook.IsAvailable(this))
				{
					lookSelectionForMultiplayerParamsAndSet.selectedLook = lookSet.GetDefaultLookForPersona(this);
				}
				config.Add(lookSelectionForMultiplayerParamsAndSet);
			}
		}
	}

	public Persona(Gender gender)
	{
		config.Clear();
		this.gender = gender;
		foreach (CustomisationConfig.LookSet lookSet in CustomisationConfig.instance.lookSets)
		{
			if (lookSet.looks.Count > 0)
			{
				LookSelection lookSelection = new LookSelection(lookSet, -1);
				if (lookSelection.selectedLook == null || !lookSelection.selectedLook.IsAvailable(this))
				{
					lookSelection.selectedLook = lookSet.GetDefaultLookForPersona(this);
				}
				config.Add(lookSelection);
			}
		}
	}

	private LookSelection GetLookSelectionForPlayerSettingsAndSet(CustomisationConfig.LookSet set, GGPlayerSettings ps)
	{
		int num = -1;
		if (set.type == CustomisationConfig.LookSetType.Shoes)
		{
			StoreItemsConfig.StoreItem itemForTypeAndId = GetItemForTypeAndId(StoreItemsConfig.StoreItem.ItemType.Shoe, ps.IdOfUsedItem(5));
			Debug.Log("Shoe: " + itemForTypeAndId.customisationLookName);
			num = set.GetLookIndexByName(itemForTypeAndId.customisationLookName, gender);
		}
		else if (set.type == CustomisationConfig.LookSetType.Racket)
		{
			StoreItemsConfig.StoreItem itemForTypeAndId2 = GetItemForTypeAndId(StoreItemsConfig.StoreItem.ItemType.Racket, ps.IdOfUsedItem(2));
			num = set.GetLookIndexByName(itemForTypeAndId2.customisationLookName, Gender.Male);
		}
		else
		{
			CharacterLookProperty characterLookProperty = ps.LookPropertyForType(set.type);
			num = characterLookProperty.activeIndex;
		}
		return new LookSelection(set, num);
	}

	private LookSelection GetLookSelectionForMultiplayerParamsAndSet(CustomisationConfig.LookSet set, MatchParameters.MatchParams.MultiplayerParams mp)
	{
		int num = -1;
		if (set.type == CustomisationConfig.LookSetType.Shoes)
		{
			StoreItemsConfig.StoreItem itemForTypeAndId = GetItemForTypeAndId(StoreItemsConfig.StoreItem.ItemType.Shoe, mp.remoteShoe.shoesId);
			num = set.GetLookIndexByName(itemForTypeAndId.customisationLookName, gender);
		}
		else if (set.type == CustomisationConfig.LookSetType.Racket)
		{
			StoreItemsConfig.StoreItem itemForTypeAndId2 = GetItemForTypeAndId(StoreItemsConfig.StoreItem.ItemType.Racket, mp.racketId);
			num = set.GetLookIndexByName(itemForTypeAndId2.customisationLookName, Gender.Male);
		}
		else
		{
			CharacterLookProperty characterLookProperty = LookPropertyForType(set.type, mp.remoteLooks);
			num = characterLookProperty.activeIndex;
		}
		return new LookSelection(set, num);
	}

	private StoreItemsConfig.StoreItem GetItemForTypeAndId(StoreItemsConfig.StoreItem.ItemType type, string id)
	{
		StoreItemsConfig.StoreItem storeItem = StoreItemsConfig.instance.FindItemWithId(id);
		if (storeItem == null)
		{
			storeItem = StoreItemsConfig.instance.GetItemsForType(type, true)[0];
		}
		return storeItem;
	}

	private CustomisationConfig.Look GetDefaultLookForType(CustomisationConfig.LookSetType type)
	{
		CustomisationConfig.LookSet lookSetForType = CustomisationConfig.instance.GetLookSetForType(type);
		return lookSetForType.GetDefaultLookForPersona(this);
	}

	public void InitToOwned(GGPlayerSettings ps)
	{
		List<LookSelection> list = new List<LookSelection>();
		list.AddRange(config);
		foreach (LookSelection item in list)
		{
			if (item.selectedLook == null)
			{
				continue;
			}
			CustomisationConfig.Look selectedLook = item.selectedLook;
			GGDebug.DebugLog("LOOK TYPE " + selectedLook.type);
			if (!selectedLook.isOwned && selectedLook.price > 0)
			{
				CustomisationConfig.LookSet lookSetForType = CustomisationConfig.instance.GetLookSetForType(selectedLook.type);
				LookSelection lookSelectionForPlayerSettingsAndSet = GetLookSelectionForPlayerSettingsAndSet(lookSetForType, ps);
				item.selectedLook = lookSelectionForPlayerSettingsAndSet.selectedLook;
				if (item.selectedLook == null || !item.selectedLook.IsAvailable(this) || (item.selectedLook.price > 0 && !item.selectedLook.isOwned))
				{
					GGDebug.DebugLog("Apply Initial Looks");
					item.selectedLook = lookSetForType.GetDefaultLookForPersona(this);
				}
			}
		}
		foreach (CustomisationConfig.LookSet lookSet in CustomisationConfig.instance.lookSets)
		{
			CustomisationConfig.Look lookForType = GetLookForType(lookSet.type);
			if (lookForType != null)
			{
				continue;
			}
			GGDebug.DebugLog("Look == null " + lookSet.type);
			if (lookSet.looks.Count > 0)
			{
				LookSelection lookSelectionForPlayerSettingsAndSet2 = GetLookSelectionForPlayerSettingsAndSet(lookSet, ps);
				if (lookSelectionForPlayerSettingsAndSet2.selectedLook == null || !lookSelectionForPlayerSettingsAndSet2.selectedLook.IsAvailable(this))
				{
					lookSelectionForPlayerSettingsAndSet2.selectedLook = lookSet.GetDefaultLookForPersona(this);
				}
				config.Add(lookSelectionForPlayerSettingsAndSet2);
			}
		}
	}

	private CharacterLookProperty LookPropertyForType(CustomisationConfig.LookSetType type, List<CharacterLookProperty> looks)
	{
		if (looks == null)
		{
			looks = new List<CharacterLookProperty>();
		}
		foreach (CharacterLookProperty look in looks)
		{
			if (look.type == (int)type)
			{
				return look;
			}
		}
		CharacterLookProperty characterLookProperty = new CharacterLookProperty();
		characterLookProperty.type = (int)type;
		characterLookProperty.activeIndex = 0;
		return characterLookProperty;
	}

	public CustomisationConfig.Look GetLookForType(CustomisationConfig.LookSetType type)
	{
		foreach (LookSelection item in config)
		{
			if (item.type == type)
			{
				return item.selectedLook;
			}
		}
		return null;
	}

	public void ChangeLook(CustomisationConfig.Look newLook)
	{
		for (int i = 0; i < config.Count; i++)
		{
			if (config[i].type == newLook.type)
			{
				config[i].selectedLook = newLook;
			}
		}
	}

	public void SavePreferences()
	{
		GGPlayerSettings.instance.Model.genderSelect = (int)gender;
		GGPlayerSettings.instance.Save();
		foreach (LookSelection item in config)
		{
			GGPlayerSettings.instance.SaveLookProperty(item.GetActiveLookIndex(), item.type);
		}
	}

	public bool IsLookActive(CustomisationConfig.Look look)
	{
		CustomisationConfig.Look lookForType = GetLookForType(look.type);
		return lookForType == look;
	}
}
